<template>
  <div>
    <el-form ref="form" :model="tb_goods" label-width="80px">
      <el-form-item label="商品编号">
        <el-input v-model="tb_goods.goodsNo" @blur="checkGoodsNo()" style="width: 400px"></el-input>
      </el-form-item>
      <el-form-item label="标题">
        <el-input v-model="tb_goods.title" style="width: 400px"></el-input>
      </el-form-item>
      <el-form-item label="价格">
        <el-input v-model="tb_goods.price"  style="width: 400px"></el-input>
      </el-form-item>

      <el-form-item label="描述">
        <el-input v-model="tb_goods.description"  style="width: 400px"></el-input>
      </el-form-item>
      <el-form-item label="库存">
        <el-input v-model="tb_goods.stock" style="width: 400px"></el-input>
      </el-form-item>


      <el-form-item label="分类">
        <el-select v-model="cid" placeholder="请选择" @change="findall2(cid)">
          <el-option
              v-for="item in list1"
              :key="item.cid"
              :label="item.cName"
              :value="item.cid">
          </el-option>
        </el-select>

        <el-select v-model="tb_goods.categoryId" placeholder="请选择">
          <el-option
              v-for="item in list2"
              :key="item.cid"
              :label="item.cName"
              :value="item.cid">
          </el-option>
        </el-select>



      </el-form-item>
      <el-form-item label="商品编号">
        <el-upload
            class="avatar-uploader"
            action="http://localhost:8083/image/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
          <img v-if="tb_goods.images" :src="tb_goods.images" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" @click="onSubmit">立即创建</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
  export default {
    data(){
      return{
        tb_goods:{
          goodsNo:"",
          images:"",
          categoryId:null
        },
        list1:[],
        list2:[],
        cid:""

      }
    },
    methods:{
      checkGoodsNo(){
        this.$http.get(`/users/checkgoodsno/${this.tb_goods.goodsNo}`)
        .then(res=>{
          if(res.data.falg){
            this.$message.success("没有重复可以添加")
          }else {
            this.$message.error("重复，不可添加，请重新填写");
            this.tb_goods.goodsNo="";
          }
        }).catch(err=>{

        })
      },

      handleAvatarSuccess(res, file) {
        this.tb_goods.images = res;
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 10;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      },
      findall(){
        this.$http.get(`/users/findall1`)
        .then(res=>{
          this.list1=res.data.data;

        }).catch(err=>{
            console.log(err);
        })
      },
      findall2(cid){
        var id= cid;
        this.list2=[];
        this.$http.get(`users/findall2/${id}`)
        .then(res=>{
          this.list2=res.data.data;
        }).catch(err=>{

        })
      },
      onSubmit(){
        this.$http.post(`/users/addgoods`,this.tb_goods)
            .then(res=>{
              if(res.data.falg){
                this.$message.success("添加成功");
                this.$router.push("/goodslist");
              }
            }).catch(err=>{
              console.log(err);
        })
      },

    },
    created() {
      this.findall();
    }
  }
</script>
<style>
.avatar-uploader .el-upload{
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>